package cn.geminis.workflow.service.controller;

import cn.geminis.workflow.service.entity.Category;
import cn.geminis.workflow.service.entity.filter.CategoryFilter;
import org.springframework.data.domain.Sort;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author Allen
 */
public class CategoryController {

    @RequestMapping(value = {"/children/", "/children/{parentId}"}, method = RequestMethod.GET)
    public List<Category> children(@PathVariable(required = false) String parentId) {
        var filter = CategoryFilter.builder();
        if (ObjectUtils.isEmpty(parentId)) {
            filter.parentIdIsNull(true);
        } else {
            filter.parentId(parentId);
        }
        return repository.findAll(filter.build(), Sort.by(Sort.Direction.ASC, "showOrder"));
    }

}
